import request from '@/utils/request';

/**
 * 获取分页列表
 */
export async function getPage(params) {
  const res = await request.get('/user/page', params);
  if (res.data.code === 1 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 获取普通列表数据
 * @param {*} params
 * @returns
 */
export async function getLists(params) {
  const res = await request.get('/user/lists', params);
  if (res.data.code === 1 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 获取树形结构的列表数据
 * @param {*} params
 * @returns
 */
export async function getTree(params) {
  const res = await request.get('/user/tree', params);
  if (res.data.code === 1 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 获取单条详情数据
 * @param {*} params
 * @returns
 */
export async function getInfo(id) {
  const res = await request.get('/user/info', { id });
  if (res.data.code === 1 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加
 * @param {*} params
 * @returns
 */
export async function add(params) {
  const res = await request.post('/user/add', params);
  if (res.data.code === 1) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改
 * @param {*} params
 * @returns
 */
export async function edit(params) {
  const res = await request.post('/user/edit', params);
  if (res.data.code === 1) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 单条删除
 * @param {*} id 删除的id
 * @returns
 */
export async function del(id) {
  const res = await request.post('/user/del', { id });
  if (res.data.code === 1) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除
 * @param {*} ids 删除的ids(array<int>)
 * @returns
 */
export async function delBatch(ids) {
  const res = await request.post('/user/delBatch', { ids });
  if (res.data.code === 1) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 单条设置状态
 * @param {*} id
 * @param {*} status
 * @returns
 */
export async function setStatus(id, status) {
  const res = await request.post('/user/setStatus', { id, status });
  if (res.data.code === 1) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量设置状态
 * @param {*} ids 删除的ids(array<int>)
 * @param {*} status
 * @returns
 */
export async function setStatusBatch(ids, status) {
  const res = await request.post('/user/setStatusBatch', { ids, status });
  if (res.data.code === 1) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导入数据
 * @param {*} params
 * @returns
 */
export async function importDatas(params) {
  const res = await request.post('/user/importDatas', params);
  if (res.data.code === 1) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出数据
 * @param {*} params
 * @returns
 */
export async function exportDatas(params) {
  const res = await request.get('/user/info', params);
  if (res.data.code === 1 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}
